import storage from '@system.storage';
import Config from './config.js';

export default {
/**
     * 保存最高分
     * @param bestScore
     */
    putBestScore(bestScore) {
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                storage.set({
                    key: Config.KEY_BEST_SCORE_WITHIN_4,
                    value: bestScore + "",
                    success: function () {
                        console.log('===putBestScore call storage.set success.');
                    },
                    fail: function (data, code) {
                        console.log('===putBestScore call storage.set fail, code: ' + code + ', data: ' + data);
                    },
                });
                break;
            }
            case 5: // 难度5
            {
                storage.set({
                    key: Config.KEY_BEST_SCORE_WITHIN_5,
                    value: bestScore + "",
                    success: function () {
                        console.log('===putBestScore call storage.set success.');
                    },
                    fail: function (data, code) {
                        console.log('===putBestScore call storage.set fail, code: ' + code + ', data: ' + data);
                    },
                });
                break;
            }
            case 6: // 难度6
            {
                storage.set({
                    key: Config.KEY_BEST_SCORE_WITHIN_6,
                    value: bestScore + "",
                    success: function () {
                        console.log('===putBestScore call storage.set success.');
                    },
                    fail: function (data, code) {
                        console.log('===putBestScore call storage.set fail, code: ' + code + ', data: ' + data);
                    },
                });
                break;
            }
            default:
                break;
        }
    },
/**
     * 获取最高分
     */
    getBestScore(successCallback, failCallback) {
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                storage.get({
                    key: Config.KEY_BEST_SCORE_WITHIN_4,
                    success: function (data) {
                        console.log('===getBestScore call storage.get success: ' + data);
                        successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
                    },
                    fail: function (data, code) {
                        console.log('===getBestScore call storage.get fail, code: ' + code + ', data: ' + data);
                        failCallback && failCallback(0);
                    },
                    complete: function () {
                        console.log('===getBestScore call complete');
                    },
                });
                break;
            }
            case 5: // 难度5
            {
                storage.get({
                    key: Config.KEY_BEST_SCORE_WITHIN_5,
                    success: function (data) {
                        console.log('===getBestScore call storage.get success: ' + data);
                        successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
                    },
                    fail: function (data, code) {
                        console.log('===getBestScore call storage.get fail, code: ' + code + ', data: ' + data);
                        failCallback && failCallback(data);
                    },
                    complete: function () {
                        console.log('===getBestScore call complete');
                    },
                });
                break;
            }
            case 6: // 难度6
            {
                storage.get({
                    key: Config.KEY_BEST_SCORE_WITHIN_6,
                    success: function (data) {
                        console.log('===getBestScore call storage.get success: ' + data);
                        successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
                    },
                    fail: function (data, code) {
                        console.log('===getBestScore call storage.get fail, code: ' + code + ', data: ' + data);
                        failCallback && failCallback(data);
                    },
                    complete: function () {
                        console.log('===getBestScore call complete');
                    },
                });
                break;
            }
            default:
                break;
        }
    },
/**
     * 保存当前得分
     * @param currentScore
     */
    putCurrentScore(currentScore) {
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                storage.set({
                    key: "KEY_CURRENT_SCORE_4",
                    value: currentScore + "",
                    success: function () {
                        console.log('===putCurrentScore call storage.set success.');
                    },
                    fail: function (data, code) {
                        console.log('===putCurrentScore call storage.set fail, code: ' + code + ', data: ' + data);
                    },
                });
                break;
            }
            case 5: // 难度5
            {
                storage.set({
                    key: "KEY_CURRENT_SCORE_5",
                    value: currentScore + "",
                    success: function () {
                        console.log('===putCurrentScore call storage.set success.');
                    },
                    fail: function (data, code) {
                        console.log('===putCurrentScore call storage.set fail, code: ' + code + ', data: ' + data);
                    },
                });
                break;
            }
            case 6: // 难度6
            {
                storage.set({
                    key: "KEY_CURRENT_SCORE_6",
                    value: currentScore + "",
                    success: function () {
                        console.log('===putCurrentScore call storage.set success.');
                    },
                    fail: function (data, code) {
                        console.log('===putCurrentScore call storage.set fail, code: ' + code + ', data: ' + data);
                    },
                });
                break;
            }
            default:
                break;
        }
    },
/**
     * 获取当前得分
     * @return bestScore
     */
    getCurrentScore(successCallback, failCallback) {
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                storage.get({
                    key: "KEY_CURRENT_SCORE_4",
                    success: function (data) {
                        console.log('===getCurrentScore call storage.get success: ' + (data));
                        successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
                    },
                    fail: function (data, code) {
                        console.log('===getCurrentScore call storage.get fail, code: ' + code + ', data: ' + data);
                        failCallback && failCallback(0);
                    },
                    complete: function () {
                        console.log('===getCurrentScore call complete');
                    },
                });
                break;
            }
            case 5: // 难度5
            {
                storage.get({
                    key: "KEY_CURRENT_SCORE_5",
                    success: function (data) {
                        console.log('===getCurrentScore call storage.get success: ' + data);
                        successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
                    },
                    fail: function (data, code) {
                        console.log('===getCurrentScore call storage.get fail, code: ' + code + ', data: ' + data);
                        failCallback && failCallback(data);
                    },
                    complete: function () {
                        console.log('===getCurrentScore call complete');
                    },
                });
                break;
            }
            case 6: // 难度6
            {
                storage.get({
                    key: "KEY_CURRENT_SCORE_6",
                    success: function (data) {
                        console.log('===getCurrentScore call storage.get success: ' + data);
                        successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
                    },
                    fail: function (data, code) {
                        console.log('===getCurrentScore call storage.get fail, code: ' + code + ', data: ' + data);
                        failCallback && failCallback(data);
                    },
                    complete: function () {
                        console.log('===getCurrentScore call complete');
                    },
                });
                break;
            }
            default:
                break;
        }
    },
/**
     * 保存无限模式下最高分
     * @param bestScore
     */
    putBestScoreWithinInfinite(bestScore) {
        storage.set({
            key: Config.KEY_BEST_SCORE_WITHIN_INFINITE,
            value: bestScore + "",
            success: function () {
                console.log('===putBestScoreWithinInfinite call storage.set success.');
            },
            fail: function (data, code) {
                console.log('===putBestScoreWithinInfinite call storage.set fail, code: ' + code + ', data: ' + data);
            },
        });
    },
/**
     * 获取无限模式下最高分
     */
    getBestScoreWithinInfinite(successCallback, failCallback) {
        storage.get({
            key: Config.KEY_BEST_SCORE_WITHIN_INFINITE,
            success: function (data) {
                console.log('===getBestScoreWithinInfinite call storage.get success: ' + data);
                successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
            },
            fail: function (data, code) {
                console.log('===getBestScoreWithinInfinite call storage.get fail, code: ' + code + ', data: ' + data);
                failCallback && failCallback(0);
            },
            complete: function () {
                console.log('===getBestScoreWithinInfinite call complete');
            },
        });
    },
/**
     * 保存当前无限模式分数
     */
    putCurrentScoreWithinInfinite(currentScore) {
        storage.set({
            key: "KEY_CURRENT_SCORE_INFINITE",
            value: currentScore + "",
            success: function () {
                console.log('===putCurrentScoreWithinInfinite call storage.set success.');
            },
            fail: function (data, code) {
                console.log('===putCurrentScoreWithinInfinite call storage.set fail, code: ' + code + ', data: ' + data);
            },
        });
    },
/**
     * 获取当前无限模式分数
     * @return currentScore
     */
    getCurrentScoreWithinInfinite(successCallback, failCallback) {
        storage.get({
            key: "KEY_CURRENT_SCORE_INFINITE",
            success: function (data) {
                console.log('===getCurrentScoreWithinInfinite call storage.get success: ' + data);
                successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
            },
            fail: function (data, code) {
                console.log('===getCurrentScoreWithinInfinite call storage.get fail, code: ' + code + ', data: ' + data);
                failCallback && failCallback(data);
            },
            complete: function () {
                console.log('===getCurrentScoreWithinInfinite call complete');
            },
        });
    },
/**
     * 保存游戏难度
     * @param difficulty
     */
    putGameDifficulty(difficulty) {
        storage.set({
            key: Config.SAVE_GAME_DIFFICULTY,
            value: difficulty + "",
            success: function () {
                console.log('===putGameDifficulty call call storage.set success.');
            },
            fail: function (data, code) {
                console.log('===putGameDifficulty call storage.set fail, code: ' + code + ', data: ' + data);
            },
        });
    },
/**
     * 获取游戏难度
     */
    getGameDifficulty(successCallback, failCallback) {
        storage.get({
            key: Config.KEY_GAME_DIFFICULTY,
            success: function (data) {
                console.log('===getGameDifficulty call storage.get success: ' + data);
                successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
            },
            fail: function (data, code) {
                console.log('===getGameDifficulty call storage.get fail, code: ' + code + ', data: ' + data);
                failCallback && failCallback(4);
            },
            complete: function () {
                console.log('===getGameDifficulty call complete');
            },
        });
    },
/**
     * 保存游戏音效状态
     * @param volumeState
     */
    putGameVolume(volumeState) {
        storage.set({
            key: Config.KEY_GAME_VOLUME_STATE,
            value: volumeState,
            success: function () {
                console.log('===putGameVolume call storage.set success.');
            },
            fail: function (data, code) {
                console.log('===putGameVolume call storage.set fail, code: ' + code + ', data: ' + data);
            },
        });
    },
/**
     * 获取游戏音效状态
     */
    getGameVolumeState(successCallback, failCallback) {
        storage.get({
            key: Config.KEY_GAME_VOLUME_STATE,
            success: function (data) {
                console.log('===getGameVolumeState call storage.get success: ' + data);
                successCallback && successCallback(data && ((typeof data) == 'boolean') ? data : true);
            },
            fail: function (data, code) {
                console.log('===getGameVolumeState call storage.get fail, code: ' + code + ', data: ' + data);
                failCallback && failCallback(true);
            },
            complete: function () {
                console.log('===getGameVolumeState call complete');
            },
        });
    },
/**
     * 保存达成游戏目标次数
     * @param time
     */
    putGoalTime(time) {
        storage.set({
            key: Config.KEY_GET_GOAL_TIME,
            value: time,
            success: function () {
                console.log('===putGoalTime call storage.set success.');
            },
            fail: function (data, code) {
                console.log('===putGoalTime call storage.set fail, code: ' + code + ', data: ' + data);
            },
        });
    },
/**
     * 获取达成游戏目标次数
     */
    getGoalTime(successCallback, failCallback) {
        storage.get({
            key: Config.KEY_GET_GOAL_TIME,
            success: function (data) {
                console.log('===getGoalTime call storage.get success: ' + data);
                successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
            },
            fail: function (data, code) {
                console.log('===getGoalTime call storage.get fail, code: ' + code + ', data: ' + data);
                failCallback && failCallback(0);
            },
            complete: function () {
                console.log('===getGoalTime call complete');
            },
        });
    },
/**
     * 保存游戏模式
     * @param mode
     */
    putCurrentGameMode(mode) {
        console.log("=== mode: " + mode)
        storage.set({
            key: Config.KEY_CURRENT_GAME_MODE,
            value: mode + "",
            success: function () {
                console.log('===putCurrentGameMode call storage.set success.');
            },
            fail: function (data, code) {
                console.log('===putCurrentGameMode call storage.set fail, code: ' + code + ', data: ' + data);
            },
        });
    },
/**
     * 获取游戏模式
     */
    getCurrentGameMode(successCallback, failCallback) {
        storage.get({
            key: Config.KEY_CURRENT_GAME_MODE,
            success: function (data) {
                console.log('===getCurrentGameMode call storage.get success: ' + data);
                successCallback && successCallback(data && !isNaN(data) ? parseInt(data) : 0);
            },
            fail: function (data, code) {
                console.log('===getCurrentGameMode call storage.get fail, code: ' + code + ', data: ' + data);
                failCallback && failCallback(0);
            },
            complete: function () {
                console.log('===getCurrentGameMode call complete');
            },
        });
    }
}